package com.demon.auto.controller;

import com.demon.auto.model.ApiResponseModel;
import com.demon.auto.model.dto.SignClientDto;
import com.demon.auto.model.entity.SignClientEntity;
import com.demon.auto.model.vo.PageVo;
import com.demon.auto.model.vo.SignClientVo;
import com.demon.auto.service.SignClientService;
import com.demon.auto.utils.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 签到客户端
 *
 * @version 1.0
 * @author huangrz
 * @date 2025/02/21 11:31
 */
@RestController
@RequestMapping("/api/signClient")
public class SignClientController {

    @Autowired
    private SignClientService service;

    @RequestMapping("/getUserSignClient")
    public ApiResponseModel<List<SignClientDto>> getUserSignClient(Long userId) {
        if (userId == null) {
            userId = CommonUtil.getCurrentUserId();
        }
        return ApiResponseModel.ok("获取用户关联的签到客户端成功", service.getUserSignClient(userId));
    }

    @RequestMapping("/saveUserSignClient")
    public ApiResponseModel<Boolean> saveUserSignClient(@Valid @RequestBody SignClientDto signClient) {
        Boolean flag = service.saveUserSignClient(signClient);
        if (flag == null) {
            return ApiResponseModel.error("客户端名称已存在，请修改名称或联系管理员共享该客户端", false);
        }
        return ApiResponseModel.ok("保存用户签到客户端", flag);
    }

    @RequestMapping("/queryByCondition")
    public ApiResponseModel<PageVo<SignClientEntity>> queryByCondition(@RequestBody SignClientVo condition) {
        return ApiResponseModel.ok("查询签到客户端信息成功", service.queryByPage(condition));
    }

    /**
     * 修改客户端有效标识
     *
     * @param clientId 客户端ID
     * @return ApiResponseModel<Boolean>
     */
    @RequestMapping("/invalid")
    public ApiResponseModel<Boolean> invalid(Long clientId) {
        if (clientId == null) {
            return ApiResponseModel.error("客户端ID不能为空", false);
        }
        return ApiResponseModel.ok("作废客户端有效标识", service.invalidUserClient(clientId, CommonUtil.getCurrentUserId()));
    }

    /**
     * 修改客户端启用标识
     * @param clientId 客户端ID
     * @return ApiResponseModel<Boolean>
     */
    @RequestMapping("/updateOpenFlag")
    public ApiResponseModel<Boolean> updateOpenFlag(Long clientId, Boolean openFlag) {
        if (clientId == null || openFlag == null) {
            return ApiResponseModel.error("客户端ID和启用标识不能为空", false);
        }
        return ApiResponseModel.ok("修改客户端启用标识",
                service.updateById(new SignClientEntity()
                        .setId(clientId)
                        .setOpenFlag(openFlag)));
    }

    @RequestMapping("/judgeClientNameIsExist")
    public ApiResponseModel<Boolean> judgeClientNameIsExist(String clientName) {
        if (StringUtils.isBlank(clientName)) {
            return ApiResponseModel.error("客户端名称不能为空！", false);
        }
        return ApiResponseModel.ok("判断客户端名称是否存在", service.judgeClientNameIsExist(clientName));
    }

}
